package game.system;

import game.db.DBUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserModel {
	//在线用户列表
	private static Map<String, UserVo> userMap = new HashMap();
	
	/**
	 * 发送信息给用户
	 * @param uid
	 * @param msg
	 */
	public static void sendToUser(String uid, String msg){
		UserVo vo = userMap.get(uid);
		if(vo != null){
			vo.getGameThread().send(msg);
		}
	}
	
	/**
	 * 发送信息给所有用户
	 * @param msg
	 */
	public static void sendToAll(String msg){
		for (String uid : userMap.keySet()) {
			sendToUser(uid, msg);
		}
	}

	public static Map getUserMap() {
		return userMap;
	}
	
	public static UserVo getUser(String uid) {
		return userMap.get(uid);
	}

	/**
	 * 添加用户
	 * @param vo
	 */
	public static void addUser(UserVo user) {
		userMap.put(user.getId(), user);
	}
	
	/**
	 * 删除用户
	 * @param uid
	 * @return
	 */
	public static UserVo removeUser(String uid) {
		return userMap.remove(uid);
	}
	

	
	public static String getCurrUserMsg(String uid){
		UserVo vo = getUser(uid);
		if(vo==null){
			return "";
		}
		StringBuffer msg = new StringBuffer();
		msg.append("&uid=");	msg.append(vo.getId());
		msg.append("&name=");	msg.append(vo.getName());
		msg.append("&ip=");	msg.append(vo.getIp());
		return msg.toString();
	}
	
}
